package com.alen.websocketDemo

import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.EditText
import android.widget.ListView
import androidx.appcompat.app.AppCompatActivity
import com.alen.websocket.SocketCallback
import com.alen.websocket.WebSocketKit
import okio.ByteString

class MainActivity : AppCompatActivity() {
    lateinit var etUrl: EditText
    lateinit var etMessage: EditText
    lateinit var btnSend: Button
    lateinit var btnConnect: Button
    lateinit var btnReConnect: Button
    lateinit var btnClose: Button
    lateinit var lvMessage: ListView
    private val url: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
        initSocket()
    }

    lateinit var adapter: MessageAdapter

    private fun initView() {
        etUrl = findViewById<EditText>(R.id.etUrl)
        etMessage = findViewById<EditText>(R.id.etMessage)
        btnSend = findViewById<Button>(R.id.btnSend)
        btnConnect = findViewById<Button>(R.id.btnConnect)
        btnReConnect = findViewById<Button>(R.id.btnReConnect)
        btnClose = findViewById<Button>(R.id.btnClose)
        lvMessage = findViewById<ListView>(R.id.lvMessage)

        adapter = MessageAdapter(this, mutableListOf())
        lvMessage.adapter = adapter

        btnSend.setOnClickListener {
            val msg = etMessage.text.toString()
            WebSocketKit.defaultClient().send(msg)
            etMessage.setText("")
            addMessage("我：", msg)
        }
        btnConnect.setOnClickListener {
            val url = etUrl.text.toString().trim()
            if (url.isNotEmpty()) {
                WebSocketKit.defaultClient().setUrl(url)
                WebSocketKit.defaultClient().startConnect()
            }
        }
        btnReConnect.setOnClickListener {
            WebSocketKit.defaultClient().reconnect()
        }
        btnClose.setOnClickListener {
            WebSocketKit.defaultClient().close()
        }

    }


    private fun addMessage(name: String, content: String) {
        adapter.addData(MessageData(name, content))
    }

    private fun initSocket() {
        WebSocketKit.init(this, true)
        WebSocketKit.defaultClient()
            .addListener(object : SocketCallback {
                override fun onOpen() {
                    Log.d("MainActivity", "**********onOpen:")
                    btnConnect.isEnabled = false
                    btnClose.isEnabled = true
                }

                override fun onMessage(message: String) {
                    Log.d("MainActivity", "**********返回数据:$message")
                    addMessage("服务器：", message)
                }

                override fun onMessage(bytes: ByteString) {
                    Log.d("MainActivity", "**********返回数据:$bytes")
                    addMessage("服务器：", bytes.base64())
                }

                override fun onReconnect() {
                    Log.d("MainActivity", "**********重连:")
                }

                override fun onClose(reason: String) {
                    Log.d("MainActivity", "**********关闭")
                    btnConnect.isEnabled = true
                    btnClose.isEnabled = false
                }
            })
    }

    override fun onDestroy() {
        super.onDestroy()
        WebSocketKit.defaultClient().close()
        WebSocketKit.reInit()
    }
}